/**
 * Created by GadflyBSD on 16/6/24.
 */
angular.module('starter.controllers', ['ionic'])
	.controller('tabCtrl', function($rootScope, myService){
		myService.getBaseData().success(function(resp){
			$rootScope.public_key = resp.public;
		});
	})
	.controller('getBaseDataCtrl', function($scope, myService){

	})
	.controller('register', function($scope, $state, myPopup, myService){
		"用户注册控制器";
		myService.getSession(true).success(function(resp){
			$scope.session = resp;
			alert(JSON.stringify(resp));
		});
		$scope.register = {toggle: false}
		$scope.submit = function(){
			if(!$scope.register.toggle){
				myPopup.alert({
					type: 'Error',
					title: '出错啦!',
					content: '您没有同意《汪卡用户领用协议》, 因而不能继续注册!'
				});
			}else{
				myService.register($scope.register).success(function(resp){
					myPopup.alert(resp);
					if(resp.errcode == 0){
						$state.go('tab.login');
					}
				});
			}
		}
	})
	.controller('loginCtrl', function($scope, $rootScope, $state, myPopup, myService){
		"用户登录控制器";
		$scope.verifyImg = baseUrl + '/App/verifyImg';
		$scope.login = {};
		$scope.refresh = function(){
			$scope.verifyImg = $scope.verifyImg + '?' + Math.random();
		}
		//console.log($rootScope);
		$scope.submit = function(){
			setMaxDigits(131); //131 => n的十六进制位数/2+3
			var key      = new RSAKeyPair("10001", '', $rootScope.public_key); //10001 => e的十六进制
			var post = JSON.stringify($scope.login);
			console.log({rsa: encryptedString(key, post)})
			/*myService.login($scope.login).success(function(resp){
				myPopup.alert(resp);
				if(resp.errcode == 0) $state.go('tab.home');
			})*/
		}
	})
	.controller('my', function($scope) {
		"个人中心控制器";
	})
	.controller('realname', function($scope, $ionicLoading, $state, myService, myPopup){
		"实名认证控制器";
		$ionicLoading.show({content: 'Loading', animation: 'fade-in', showBackdrop: true, maxWidth: 200, showDelay: 0});
		$scope.realname = {
			idcard_url_1: 'notImg',
			idcard_url_2: 'notImg',
			student_card_url: 'notImg',
		}
		myService.getSession(true).success(function(resp){
			$ionicLoading.hide();
			if(resp.errcode != '0'){
				var alertPopup = myPopup.alert(resp);
				alertPopup.then(function(){$state.go('tab.login');});
			}else{
				$scope.realname = angular.extend($scope.realname, resp.data);
				$scope.school = resp.school.data;
				$scope.surveySelect = function(){
					$ionicLoading.show({content: 'Loading', animation: 'fade-in', showBackdrop: true, maxWidth: 200, showDelay: 0});
					myService.getSurvey($scope.realname.school.catname).success(function(res){
						$scope.survey = res.data;
						$ionicLoading.hide();
					});
				}
				$scope.submit = function(){
					$ionicLoading.show({content: 'Loading', animation: 'fade-in', showBackdrop: true, maxWidth: 200, showDelay: 0});
					myService.setRealname($scope.realname).success(function(resp){
						$ionicLoading.hide();
						myPopup.alert(resp);
						if(resp.errcode == 0) $state.go('tab.my');
					});
				}
			}
		});
	})
	.controller('my_bangzhuzhongxinc',my_bangzhuzhongxinc)
	.controller('haikuangcc', function($scope) {
		$scope.$on('$ionicView.enter', function() {
			// alert(8877)

		})
		$scope.haikuangmingxi=function(){
			// alert(gindex)
			if(gindex==1){
				gurl('#/tab/haikuangmingxi')
				return
			}
			if(gindex==2){
				gurl('#/tab/haikuangmingxi2')
				return
			}
		}

		$scope.haikuang=function(){
			// alert(gindex)
			// return
			if(gindex==1){
				gurl('#/tab/home_jilu')
				return
			}
			if(gindex==2){
				gurl('#/tab/account_jilu')
				return
			}
		}
	})
	.controller('tabc',function($scope){
		var arr=[1,"#/tab/home","#/tab/account","#/tab/discovery","#/tab/my"]
		$scope.click=function(a1){
			gindex=a1;
			var url=arr[a1];
			gurl(url);
		}
		// gurl('#/tab/home');
		// alert(999)
	})
	.controller('home_haikuanjilucc', function($scope) {
		// alert(999)
		$scope.click=function(ss){
			$('#home1').removeClass('active')
			$('#home2').removeClass('active')
			$(ss).addClass('active')

		}
	})
	.controller('homec', function($scope,$ionicModal,ss,myPopup,$timeout, myService) {

		// $http.get("http://www.runoob.com/try/angularjs/data/Customers_JSON.php")
		//   .success(function(response) {alert(88)});

		// myService.getSession().success(function(resp){
		//     alert(JSON.stringify(resp));
		// })

		// myService.test('ee44').success(function(resp){
		//     alert(resp.name);
		// })



		// $scope.items=[{'aa':vt,'bb':1},{'aa':2,'bb':2}];
		$scope.$on('$ionicView.enter', function() {
			$scope.log=3333;
			// mm.log()


			//mlog($scope,$ionicModal,myService,myPopup)

			//mzhuce($scope,$ionicModal,myService,myPopup)



			// $scope.modal.show()


			$scope.go=function(){
				if(islog==false){
					$scope.openModal();
				}else{
					// $scope.openModalzhuce();

				}
			}

			$('.hhh').removeClass('tabs-item-hide');
			// $scope.aa.cc='tabs-item-hide'
			// agk()
			ss.s1()
			// alert(999)
			// $scope.edu-=10
			//TweenLite.from($('.window'),1,{alpha:0})
		})

		$scope.edu=1000;

		$scope.dd={
			aa:'ffffdsfds98',
			bb:99
		}

		$scope.doRefresh=function () {
			$timeout(function () {
				$scope.edu-=10

				$scope.$broadcast('scroll.refreshComplete');
			}, 1000);
		}


		$scope.haikuang=function(){
			//$scope.$broadcast('scroll.refreshComplete');
			window.location.href='#/tab/haikuang'
			// #/tab/my
		}
	})
	.controller('home_jiekuangc', function($scope) {

	})
	.controller('haikuangc', function($scope) {
		$scope.dd={

			aa:6,
		}
		//$('.hhh').addClass('tabs-item-hide');

		// removeClass
		$scope.$on('$ionicView.enter', function() {

			$('.hhh').addClass('tabs-item-hide');
			//setTimeout($scope.hh, 500 )
			// agk()
		});

		$scope.hh=function(){
			// alert(99)
			$scope.dd.aa+=35
		}
		// setTimeout(function(){}, 3000 )
	})
	.controller('accountc', function($scope) {
		//$('.hhh').removeClass('tabs-item-hide');
		$scope.$on('$ionicView.enter', function() {

			$('.hhh').removeClass('tabs-item-hide');
			// agk()
		});
	})
	.controller('discoveryc', function($scope) {
		$scope.$on('$ionicView.enter', function() {

			$('.hhh').removeClass('tabs-item-hide');
		});
		$scope.settings = {
			enableFriends: true
		};
	});